package com.usian.controller;

import com.usian.GoodsFeign;
import com.usian.entity.PageResult;
import com.usian.entity.QueryPageBean;
import com.usian.entity.Result;
import com.usian.pojo.Goods;
import com.usian.pojo.Role;
import com.usian.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    GoodsFeign goodsFeign;

    @RequestMapping("/{page}")
    public String page(@PathVariable String page, HttpSession session,Model model){
        User user =(User) session.getAttribute("user");
        model.addAttribute("user",user);
        return page;
    }

    @RequestMapping("/login")
    @ResponseBody
    public Result login(@RequestBody User user, HttpSession session){
        session.setAttribute("user",user);
        List<User> users = goodsFeign.showList(user);
        if (users!=null){
            return new Result("ok",0);
        }
        return new Result("去注册吧你",1);
    }

    @RequestMapping("/register")
    @ResponseBody
    public Result register(@RequestBody User user){
        goodsFeign.register(user);
        return new Result("ok",0);
    }

    @RequestMapping("/findPage")
    @ResponseBody
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean,HttpSession session){
        User user =(User) session.getAttribute("user");
        HashMap queryString = queryPageBean.getQueryString();
        queryString.put("roleId",user.getRoleId());
        return goodsFeign.pageList(queryPageBean);
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Result delete(@RequestParam Integer id){
        goodsFeign.delete(id);
        return new Result("ok",0);
    }

    @RequestMapping("/add")
    @ResponseBody
    public Result add(@RequestBody Goods goods){
        goodsFeign.add(goods);
        return new Result("ok",0);
    }

    @RequestMapping("/update")
    @ResponseBody
    public Result update(@RequestBody Goods goods){
        goodsFeign.update(goods);
        return new Result("ok",0);
    }
    @RequestMapping("/updateStatus")
    @ResponseBody
    public Result updateStatus(@RequestParam Integer id, @RequestParam Integer status){
        goodsFeign.updateStatus(id,status);
        return new Result("ok",0);
    }

    @RequestMapping("/findRole")
    @ResponseBody
    public List<Role> findRole(){
        return goodsFeign.findRole();
    }

}
